﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "CSVFileListModel.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

CSVFileListModel::CSVFileListModel(gcmp::IDocument* pDoc,gcmp::OwnerPtr<gfam::ICSVFileManager> csvFileManager,QObject*parent):QAbstractListModel(parent),m_pDoc(pDoc) ,m_csvFileManager(TransferOwnership(csvFileManager))
{
}

CSVFileListModel::~CSVFileListModel()
{
    m_csvFileManager = nullptr;
}

int CSVFileListModel::rowCount(const QModelIndex & parent) const
{
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager,0, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    int count = m_csvFileManager->GetFileCount();
    if (count<0)
    {
        return 0;
    }
    return count;
}

QVariant CSVFileListModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    if (role==Qt::DisplayRole)
    {
        DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, L"", L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
        std::wstring name = m_csvFileManager->GetFileNameByIndex(index.row());
        if (name.empty())
        {
            name = std::to_wstring(index.row());
        }
        return QString::fromStdWString(name);
    }
    return QVariant();
}

bool CSVFileListModel::importCSVFile(const std::wstring& fileName,const std::wstring & filePath, std::wstring & error)
{
    bool result = false;
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, false, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    int index = m_csvFileManager->GetFileCount();
    if (index<0)
    {
        return false;
    }
    beginInsertRows(QModelIndex(), index, index);
    result = m_csvFileManager->ImportCSVFile(fileName,filePath, error);
    endInsertRows();
    return result;
}

bool CSVFileListModel::exportCSVFile(int index, const std::wstring & filePath,  std::wstring & error)
{
    if (index<0)
    {
        return false;
    }
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, false, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    std::wstring name = m_csvFileManager->GetFileNameByIndex(index);
    m_csvFileManager->ExportCSVFile(name,filePath,error);
    return true;
}

bool CSVFileListModel::deleteCSVFile(int index, std::wstring& name, std::wstring& error)
{
    bool result = false;
    if (index<0)
    {
        return false;
    }
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, false, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    name = m_csvFileManager->GetFileNameByIndex(index);
    if (name.empty())
    {
        return false;
    }
    beginRemoveRows(QModelIndex(), index, index);
    result = m_csvFileManager->DeleteCSVFile(name,error);
    endRemoveRows();
    return result;
}

bool CSVFileListModel::IsCSVFileExist(const std::wstring & name)
{
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, false, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    return m_csvFileManager->IsCSVFileExist(name);
}

std::wstring CSVFileListModel::GetFileNameByIndex(int index)
{
    if (index < 0)
    {
        return L"";
    }
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager,L"", L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    return m_csvFileManager->GetFileNameByIndex(index);
}

int CSVFileListModel::GetFileIndexByName(const std::wstring & name)
{
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, -1, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    return m_csvFileManager->GetFileIndexByName(name);
}

bool CSVFileListModel::LoadDefaultCSVFile(const std::wstring& defaultFileDir, std::wstring& error)
{
    DBG_WARN_AND_RETURN_UNLESS(m_csvFileManager, true, L"m_csvFileManager为空", L"paramodel", L"2024-02-20");
    return m_csvFileManager->LoadDefaultCSVFile(defaultFileDir,error);
}
